import random

import PhoneNumber
import basestation
import Number
import GeometryGrid
from datetime import datetime


# 生成字段
# spcode
# event 88
# cgi
# lai
# ci
# longitude
# latitude
# extra_longitude
# extra_latitude
# begintime
# oldcgi
# oldlongitude
# oldlatitude
# extra_oldlongitude
# extra_oldlatitude
# state 0
# cp


def generateCgiData():
    event = 88
    state = 0
    network = 4

    tacs = [Number.randomNumber(4) for i in range(30)]
    # phonenumber, spcode = PhoneNumber.generate_phone_number()
    # 生成基站列表数据

    cgiFile = open("C:/Users/28256/PycharmProjects/pythonProject/data/cgi.csv", "w")

    cgiFile.write("spcode,cgi,tac,longcid,lng,lat\n")
    for i in range(10000):
        tac = random.choice(tacs)
        # 23.06863°,113.27779° : 23.11789°,113.34718°
        bs = basestation.generateBaseStation(network, tac, 113.27779, 113.34718, 23.06863, 23.11789)
        cgiFile.write(str(bs.spcode) + ",")
        cgiFile.write(bs.getBaseStation() + ",")
        cgiFile.write(tac + ",")
        cgiFile.write(str(bs.longCellid) + ",")
        cgiFile.write(str(bs.longitude) + ",")
        cgiFile.write(str(bs.latitude) + "\n")
    cgiFile.close()


def generateTrack():
    strdate = "20240101"
    starttimestamp = datetime.strptime(strdate, '%Y%m%d').timestamp()
    spcode1bsCoordMap = {}
    spcode1geohashBsMap = {}
    spcode2bsCoordMap = {}
    spcode2geohashBsMap = {}
    spcode3bsCoordMap = {}
    spcode3geohashBsMap = {}
    geohashset = set()
    allCgiMap = {}
    gridlevel = 7
    cgiFile = open("C:/Users/28256/PycharmProjects/pythonProject/data/cgi.csv", "r")
    i = 0
    for line in cgiFile:
        if i > 0:
            a = line.strip().split(",")
            spcode = a[0]
            cgi = a[1]
            tac = a[2]
            longcid = a[3]
            lng = float(a[4])
            lat = float(a[5])
            print(spcode, cgi, tac, longcid, lng, lat)
            if spcode == "1":
                spcode1bsCoordMap[cgi] = (lng, lat)
                geohash = GeometryGrid.geohashEncode(lat, lng, gridlevel)
                geohashset.add(geohash)
                allCgiMap[cgi] = (spcode, tac, longcid, lng, lat)
                if spcode1geohashBsMap.get(geohash) is None:
                    gridcgilist = []
                    gridcgilist.append(cgi)
                    spcode1geohashBsMap[geohash] = gridcgilist
                else:
                    gridcgilist = spcode1geohashBsMap[geohash]
                    gridcgilist.append(cgi)
            elif spcode == "2":
                spcode2bsCoordMap[cgi] = (lng, lat)
                geohash = GeometryGrid.geohashEncode(lat, lng, gridlevel)
                geohashset.add(geohash)
                allCgiMap[cgi] = (spcode, tac, longcid, lng, lat)
                if spcode2geohashBsMap.get(geohash) is None:
                    gridcgilist = []
                    gridcgilist.append(cgi)
                    spcode2geohashBsMap[geohash] = gridcgilist
                else:
                    gridcgilist = spcode2geohashBsMap[geohash]
                    gridcgilist.append(cgi)
            else:
                spcode3bsCoordMap[cgi] = (lng, lat)
                geohash = GeometryGrid.geohashEncode(lat, lng, gridlevel)
                geohashset.add(geohash)
                allCgiMap[cgi] = (spcode, tac, longcid, lng, lat)
                if spcode3geohashBsMap.get(geohash) is None:
                    gridcgilist = []
                    gridcgilist.append(cgi)
                    spcode3geohashBsMap[geohash] = gridcgilist
                else:
                    gridcgilist = spcode3geohashBsMap[geohash]
                    gridcgilist.append(cgi)
        i = i + 1
    cgiFile.close()

    trackFile = open("C:/Users/28256/PycharmProjects/pythonProject/data/track.csv", "w")
    trackFile.write("usernum,spcode,cgi,geohash,lai,ci,longitude,latitude,minute,cp,begintime,tmpspcode\n")
    geohash6 = ""
    phonenumber, spcode = PhoneNumber.generate_phone_number()

    for i in range(1, 140):
        minuteTime = datetime.fromtimestamp(starttimestamp).strftime('%Y%m%d%H%M')
        hourTime = datetime.fromtimestamp(starttimestamp).strftime('%Y%m%d%H')
        if spcode == "1":
            cgiList = list(spcode1bsCoordMap.keys())
            cgi = random.choice(cgiList)
            lng, lat = spcode1bsCoordMap[cgi]
            if geohash6 == "":
                geohash6 = GeometryGrid.geohashEncode(lat, lng, gridlevel)
            else:
                neighborList = GeometryGrid.geohashNeighbors(geohash6)
                neighborGeohashList = list(neighborList.values())
                tmpgeohash = random.choice(neighborGeohashList)
                if geohashset.__contains__(tmpgeohash):
                    geohash6 = tmpgeohash
            cgiList = spcode1geohashBsMap.get(geohash6, )
            if cgiList is not None:
                cgi = random.choice(cgiList)
                tmpspcode, tac, longcid, lng, lat = allCgiMap[cgi]
                trackFile.write(phonenumber + ",")
                trackFile.write(str(spcode) + ",")
                trackFile.write(cgi + ",")
                trackFile.write(geohash6 + ",")
                trackFile.write(tac + ",")
                trackFile.write(longcid + ",")
                trackFile.write(str(lng) + ",")
                trackFile.write(str(lat) + ",")
                trackFile.write(minuteTime + ",")
                trackFile.write(hourTime + ",")
                trackFile.write(str(int(starttimestamp)) + ",")
                trackFile.write(tmpspcode + "\n")
        elif spcode == "2":
            cgiList = list(spcode2bsCoordMap.keys())
            cgi = random.choice(cgiList)
            lng, lat = spcode2bsCoordMap[cgi]
            if geohash6 == "":
                geohash6 = GeometryGrid.geohashEncode(lat, lng, gridlevel)
            else:
                neighborList = GeometryGrid.geohashNeighbors(geohash6)
                neighborGeohashList = list(neighborList.values())
                tmpgeohash = random.choice(neighborGeohashList)
                if geohashset.__contains__(tmpgeohash):
                    geohash6 = tmpgeohash
            cgiList = spcode2geohashBsMap.get(geohash6, )
            if cgiList is not None:
                cgi = random.choice(cgiList)
                tmpspcode, tac, longcid, lng, lat = allCgiMap[cgi]
                trackFile.write(phonenumber + ",")
                trackFile.write(str(spcode) + ",")
                trackFile.write(cgi + ",")
                trackFile.write(geohash6 + ",")
                trackFile.write(tac + ",")
                trackFile.write(longcid + ",")
                trackFile.write(str(lng) + ",")
                trackFile.write(str(lat) + ",")
                trackFile.write(minuteTime + ",")
                trackFile.write(hourTime + ",")
                trackFile.write(str(int(starttimestamp)) + ",")
                trackFile.write(tmpspcode + "\n")
        else:
            cgiList = list(spcode3bsCoordMap.keys())
            cgi = random.choice(cgiList)
            lng, lat = spcode3bsCoordMap[cgi]
            if geohash6 == "":
                geohash6 = GeometryGrid.geohashEncode(lat, lng, gridlevel)
            else:
                neighborList = GeometryGrid.geohashNeighbors(geohash6)
                neighborGeohashList = list(neighborList.values())
                tmpgeohash = random.choice(neighborGeohashList)
                if geohashset.__contains__(tmpgeohash):
                    geohash6 = tmpgeohash
            cgiList = spcode3geohashBsMap.get(geohash6, )
            if cgiList is not None:
                cgi = random.choice(cgiList)
                tmpspcode, tac, longcid, lng, lat = allCgiMap[cgi]
                trackFile.write(phonenumber + ",")
                trackFile.write(str(spcode) + ",")
                trackFile.write(cgi + ",")
                trackFile.write(geohash6 + ",")
                trackFile.write(tac + ",")
                trackFile.write(longcid + ",")
                trackFile.write(str(lng) + ",")
                trackFile.write(str(lat) + ",")
                trackFile.write(minuteTime + ",")
                trackFile.write(hourTime + ",")
                trackFile.write(str(int(starttimestamp)) + ",")
                trackFile.write(tmpspcode + "\n")
        starttimestamp = starttimestamp + 600.0
    trackFile.close()


if __name__ == '__main__':
    generateTrack()
